package com.example.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.io.File;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private  File myDirectory;



    public ImageAdapter(Context c) {
        mContext = c;
        myDirectory = new File(Environment.getExternalStorageDirectory() + "/AppImages/");


    }

    public int getCount() {
        File[] photos = myDirectory.listFiles();
        if(photos == null){
            return 0;
        }
        return photos.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        File myDirectory = new File(Environment.getExternalStorageDirectory() + "/AppImages/");
        File[] photos = myDirectory.listFiles();

        File photo = photos[position];
        Bitmap b = BitmapFactory.decodeFile(photo.getAbsolutePath());

        i.setImageBitmap(b);

        i.setLayoutParams(new GridView.LayoutParams(100, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);



        return i;
    }

    // references to our images
    private Integer[] mThumbIds = {
           // R.drawable.abc,
           // R.drawable.def,
    };
}